<?php

namespace app\model;

use think\Model;

class StationPriceTask extends Model
{
    public function addOrUpdata(): array
    {
        $param = request()->post();
        if ($param['now'] == 1) {
            $param['start_time'] = date("Y-m-d H:i:s");
        }

        //获取到选中城市的油站
        $param['station_id'] = StationBasic::when(!empty($param['city']), function ($query) use ($param) {
            $query->whereIn("city_code", $param['city']);
        })->column("id");
        if (!empty($param['city'])) {
            $param['city_name'] = SysRegion::whereIn("adcode", $param['city'])->column("name");
        }
        $param['state'] = 1;
        unset($param['now']);
        $this->exists(!empty($param['id']))->save($param);
        return $this->toArray();
    }

    public function lst(): array
    {
        $param = request()->param();
        return pageData($this->order(['state' => 'asc', 'create_time' => 'desc'])->paginate([
            'list_rows' => $param['size'] ?? 30,
            'page' => $param['page'] ?? 1
        ]));
    }
}
